<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\ApplyReason;
use app\common\model\Area;
use app\common\model\DepartmentBasic;
use app\common\model\Hospital;
use app\common\model\Service as modelService;
use app\common\model\ServiceBasic;
use app\common\model\WorkerInfo;
use app\common\model\User;
use app\common\service\Amap;

class Service extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 通过经纬度获得省市区信息
     */
    public function getArea(){
        $lon=request()->param('lon');
        $lat=request()->param('lat');

        $area= (new Amap)->regeo($lon,$lat);

        if(!$area)
            $this->error('获取地址信息出错');

        $province=$area['regeocode']['addressComponent']['province']??'';
        $city=$area['regeocode']['addressComponent']['city']??'';

        $this->success('成功',['province'=>$province,'city'=>$city]);
    }

    /**
     * 基础服务列表
     */
    public function basicService(){
        $user=$this->auth->getUser();
        $type=request()->param('type');

        $banner='';

        switch ($type){
            case 'ward':
                $where=[
                    ['code','in',['ward','surgery_ward','special_ward']]
                ];
                $banner=config('site.escort_list_banner');
                break;
            case 'escort':
                $where=[
                    ['code','in',['escort','escort_vip','escort_halfday','escort_overtime']]
                ];
                $banner=config('site.escort_list_banner');
                break;
            case 'recall':
                $where=[
                    ['code','in',['severe_illness','oversea','private_doctor','famous_doctor']]
                ];
                $banner=config('site.recall_list_banner');
                break;
            case 'other':
                $where=[
                    ['code','in',['register','inquiry','document','medicine','records']]
                ];
                $banner=config('site.other_list_banner');
                break;
            default:
                $where=[
                    ['code','<>','']
                ];
                break;
        }

        if(!$banner){
            $banner=config('site.service_list_banner')?:'';
        }

        $list=ServiceBasic::where($where)->select()?:[];

        foreach ($list as &$l){
            $l->visible(['id','name','code','thumb','description','tag','base_price','day_price1','day_price2','day_price3']);
            ServiceBasic::realPrice($l,$user?$user['channel']:0);
        }

        //$banner=Config::get('site.jypz_banner');

        //$this->success('成功',$list);
        $this->success('成功',['list'=>$list,'banner'=>$banner]);
    }


    /**
     * 基础服务信息
     */
    public function basicServiceInfo(){
        $user=$this->auth->getUser();
        $code=request()->param('code');

        $info=ServiceBasic::where(['code'=>$code])->find()?:[];

        if(!$info){
            $this->error('未找到此服务信息');
        }

        ServiceBasic::realPrice($info,$user?$user['channel']:0);

        $this->success('成功',$info);
    }


    /**
     * 获取已开通地区
     */
    public function openArea(){

        $list= (new Area)->getOpenTree();

        $this->success('成功',$list);
    }


    /**
     * 医院列表
     */
    public function hospitalList(){
        $city=request()->param('city','');

        $city_info=Area::where([['name','like',"%$city%"],['level','=','2']])->find()?:[];

        if(!$city_info)
            $this->error('未找到对应城市');

        $list=Hospital::where(['status'=>'normal','city'=>$city_info['id']])->select()?:[];

        $this->success('成功',$list);
    }

    /**
     * 医院信息
     */
    public function hospitalInfo(){
        $hospital_id=request()->param('hospital_id',0);

        $hospital=Hospital::get(['id'=>$hospital_id,'status'=>'normal'])?:[];

        if(!$hospital)
            $this->error('未找到医院信息');

        $this->success('成功',$hospital);
    }

    /**
     * 医院服务列表
     */
    public function hospitalService(){
        $user=$this->auth->getUser();
        $hospital_id=request()->param('hospital_id',0);

        $list=modelService::alias('S')
            ->leftJoin('service_basic SB','S.basic_id=SB.id')
            ->where([
                'S.hospital_id'=>$hospital_id,
                'S.status'=>'normal',
                'SB.status'=>'normal'
            ])
            ->field('SB.name,SB.thumb,SB.code,SB.description,SB.tag,SB.base_price,SB.day_price1,SB.day_price2,SB.day_price3,S.*')
            ->select()?:[];


        foreach ($list as &$l){
//            $l->visible([
//                '*',
//                'service_basic'=>['id','name','code','thumb','description','tag','base_price','day_price1','day_price2','day_price3']
//            ]);
//            ServiceBasic::realPrice($l['service_basic'],$user?$user['channel']:0);
            ServiceBasic::realPrice($l,$user?$user['channel']:0);
        }


        $hospital=Hospital::get(['id'=>$hospital_id])?:[];

        $this->success('成功',['list'=>$list,'banner'=>$hospital['banner_arr']]);
    }

    /**
     * 医院服务详情
     */
    public function serviceInfo(){
        $user=$this->auth->getUser();
        $service_id=request()->param('service_id',0);

        $info=modelService::alias('S')
            ->leftJoin('service_basic SB','S.basic_id=SB.id')
            ->where([
                'S.id'=>$service_id,
                'S.status'=>'normal',
                'SB.status'=>'normal'
            ])
            ->field('SB.name,SB.thumb,SB.code,SB.description,SB.tag,SB.base_price,SB.day_price1,SB.day_price2,SB.day_price3,SB.order_notice,S.*')
            ->find()?:[];

        if(!$info)
            $this->error('服务不存在');

        $hospital=Hospital::get(['id'=>$info['hospital_id'],'status'=>'normal'])?:[];
        if(!$hospital)
            $this->error('未找到医院信息');

        $info['hospital']=$hospital;

        ServiceBasic::realPrice($info,$user?$user['channel']:0);

        $this->success('成功',$info);
    }

    public function codeHospital(){
        $code=request()->param('code');
        $city=request()->param('city');

        if(!$code)
            $this->error('请选择服务类型');

        $basic_service=ServiceBasic::where(['code'=>$code])->find()?:[];
        if(!$basic_service)
            $this->error('未找到此服务信息');

        $where=['H.status'=>'normal','S.status'=>'normal','S.basic_id'=>$basic_service['id']];

        if($city){
            $city_info=Area::where(['name'=>$city])->find()?:[];
            if(!$city_info)
                $this->error('未找到此城市的信息');
            $where['city']=$city_info['id'];
        }


        $list=Hospital::alias('H')
            ->join('service S','S.hospital_id=H.id')
            ->where($where)
            ->field('H.*,S.id as service_id')
            ->select()?:[];

        $this->success('成功',$list);
    }

    public function department(){
        $hospital_id=request()->param('hospital_id');

        $hospital=Hospital::where(['id'=>$hospital_id,'status'=>'normal'])->find()?:[];

        if(!$hospital)
            $this->error('未找到此医院信息');

        $departments=DepartmentBasic::where(['id'=>explode(',',$hospital['department']),'status'=>'normal'])
            ->order('pid asc')
            ->select()->toArray()?:[];

        $list=[];
        foreach ($departments as $d){
            if($d['pid']==0)
                $list[$d['id']]=$d;
            else
                $list[$d['pid']]['sub_department'][]=$d;
        }

        $this->success('成功',array_values($list));
    }

    public function applyReason(){
        $hospital=ApplyReason::order('weigh desc')->select()?:[];

        $this->success('成功',$hospital);
    }


    /**
     * 陪诊员列表
     */
    public function worker(){
        $code=request()->param('code')?:'';
        $page=request()->param('page')?:1;
        $city=request()->param('city');

        $gender=request()->param('gender');
        $experience=request()->param('experience');
        $age=request()->param('gender');

        if(!$city)
            $this->error('缺少所在城市信息');

        $city_info=Area::where([['name','like',"%$city%"],['level','=','2']])->find()?:[];

        if(!$city_info)
            $this->error('未找到对应城市');

        $where=[
            'U.status'=>'normal',
            'U.is_spare'=>1,
            'WF.city'=>$city_info['id'],
        ];
        $order='U.working_status desc,U.is_spare desc';


        if(in_array($code,['escort','escort_vip','escort_halfday','escort_overtime','register','inquiry','document','medicine','records'])){
            $where['WF.pzy_status']='normal';
        }
        else if(in_array($code,['ward','special_ward','surgery_ward'])){
            $where['WF.phy_status']='normal';
        }
        else{
            $this->success('成功',[]);
        }

        if($gender){
            $where['WF.gender']=$gender;
        }
        if($experience){
            $where['WF.experience']=$experience;
        }
        if($age==1){
            $order.=',WF.age asc';
        }
        if($age==2){
            $order.=',WF.age desc';
        }


        $WI=new WorkerInfo();
        $worker_list= $WI->alias('WF')
            ->leftJoin('user U','WF.user_id=U.id')
            ->where($where)
            ->field('WF.*,U.working_status,U.is_spare,U.worker_score,U.avatar')
            ->order($order)
            ->page($page,10)
            ->select();

        foreach ($worker_list as &$worker){
            $worker->hidden(['id_no','id_card_front','id_card_back','hold_id_card']);
            $worker['spare_status']= (new User)->fetchSpareStatus($worker);
        }


        $this->success('成功',$worker_list);
    }
}